# https://www.jb51.net/article/183106.htm
# https://www.runoob.com/python/python-reg-expressions.html
# (re) 对正则表达式分组并记住匹配的文本
import os
import re

import requests

if __name__ == "__main__":
    if not os.path.exists('./image'):
        os.mkdir('./image')
    # ua伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
    }
    for pageNum in range(1, 3):
        url = "https://findicons.com/pack/2787/beautiful_flat_icons/{}"
        newUrl = url.format(pageNum)
        responseText = requests.get(url=newUrl, headers=headers).text
        ex = r'<img src="([^"]+\.png)"'
        imageList = re.findall(ex, responseText)
        for img in imageList:
            imageName = img.split("/")[-1]
            imageData = requests.get(img, headers=headers).content
            with open("./image/" + imageName, "wb") as fp:
                fp.write(imageData)
                print(imageName, "下载成功！！！")

# demo
# https://zhuanlan.zhihu.com/p/45645693
# https://my.oschina.net/liurihui/blog/3071934
